﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunboxsoft.WinForm
{
    public class ModuleRuntimeContext
    {
        public List<ActionNode> Roots { get; set; }


        /// <summary>
        /// 菜单
        /// </summary>
        public List<ActionNode> Menus { get; set; }

        /// <summary>
        /// 状态栏上的动作（左）
        /// </summary>
        public ActionNodeCollection StatusBarLeftActions { get; set; }

        /// <summary>
        /// 状态栏上的动作（右）
        /// </summary>
        public ActionNodeCollection StatusBarRightActions { get; set; }

        /// <summary>
        /// 默认的ActionNode，系统第一个打开的页面
        /// </summary>
        public ActionNode DefaultActionNode { get; private set; }

        public ModuleRuntimeContext()
        {
            Roots = new List<ActionNode>();
            Menus = new List<ActionNode>();
            StatusBarLeftActions = new ActionNodeCollection();
            StatusBarRightActions = new ActionNodeCollection();
        }


        //public ActionNode RegisterAction(string name, string caption)
        //{
        //    ActionNode node = new ActionNode(name, caption);
        //    Roots.Add(node);
        //    return node;
        //}

        public ActionNode AddMenu(ActionNode action)
        {
            Menus.Add(action);
            return action;
        }

        public ActionNode AddAction(ActionNode action)
        {
            Roots.Add(action);
            return action;
        }

        public void SetDefaultAction(string name)
        {
            ActionNode action = null;

            Queue<ActionNode> stack = new Queue<ActionNode>();
            for (int index = 0; index < Roots.Count; index++)
            {
                stack.Enqueue(Roots[index]);
            }

            while (stack.Count > 0)
            {
                ActionNode node = stack.Dequeue();

                if (string.Equals(node.Name, name))
                {
                    action = node;
                    break;
                }

                if (node.Children != null)
                {
                    for (int index = 0; index < node.Children.Count; index++)
                    {
                        stack.Enqueue(node.Children[index]);
                    }
                }
            }

            if (action == null)
            {
                return;
            }
            DefaultActionNode = action;
        }
    }
}
